package com.dayouzi.crawler_monitor.config.esConfig;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class ElasticsearchClientProvider {
    
    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private Integer port;
    
    @Value("${elasticsearch.scheme}")
    private String scheme;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient esClient() {
        RestClientBuilder builder = RestClient.builder(
                new HttpHost(host, port, scheme))
                .setHttpClientConfigCallback(httpClientBuilder -> {
                    httpClientBuilder.setMaxConnTotal(100); // 设置最大连接数
                    httpClientBuilder.setMaxConnPerRoute(100); // 设置每个路由的最大连接数
                    // 可以进行其他的自定义配置，例如设置超时时间、认证等
                    return httpClientBuilder;
                });

        return new RestHighLevelClient(builder);
    }
}
